﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ZSZ.Core.Entities;

namespace ZSZ.EntityFramework.ModelConfig
{
    class AdminUserConfig : EntityTypeConfiguration<AdminUserEntity>
    {
        //public AdminUserConfig()
        //{
        //    ToTable("T_AdminUsers");

        //    //一般配置到“多”端，因为“一端”可能根本不知道“多端”的存在
        //    HasOptional(u => u.City).WithMany().HasForeignKey(u => u.CityId)
        //        .WillCascadeOnDelete(false);

        //    HasMany(u => u.Roles).WithMany(r => r.AdminUsers).Map(m=>m.ToTable("T_AdminUserRoles")
        //        .MapLeftKey("AdminUserId").MapRightKey("RoleId"));
        //    Property(e => e.Name).HasMaxLength(50).IsRequired();
        //    Property(e => e.Email).HasMaxLength(30).IsRequired().IsUnicode(false);//varchar(30)
        //    Property(e => e.PhoneNum).HasMaxLength(20).IsRequired().IsUnicode(false);
        //    Property(e => e.PasswordSalt).HasMaxLength(20).IsRequired().IsUnicode(false);
        //    Property(e => e.PasswordHash).HasMaxLength(100).IsRequired().IsUnicode(false);
        //}

        public override void Map(EntityTypeBuilder<AdminUserEntity> builder)
        {
            builder.ToTable("T_AdminUsers");
            //builder.HasOne(e => e.City).WithMany().HasForeignKey(u => u.CityId).OnDelete(DeleteBehavior.Restrict);
            //builder.HasMany(u => u.Roles).
            builder.HasMany(e => e.AdminUserRoles).WithOne(u => u.AdminUser).HasForeignKey(h => h.AdminUserId)
                .OnDelete(DeleteBehavior.ClientSetNull);
            builder.Property(e => e.Name).HasMaxLength(50).IsRequired();
            builder.Property(e => e.Email).HasMaxLength(30).IsRequired().IsUnicode(false);    //varchar(30)
            builder.Property(e => e.PhoneNum).HasMaxLength(20).IsRequired().IsUnicode(false);
            builder.Property(e => e.PasswordSalt).HasMaxLength(20).IsRequired().IsUnicode(false);
            builder.Property(e => e.PasswordHash).HasMaxLength(100).IsRequired().IsUnicode(false);


        }
    }
}
